﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using BK.DomainService;
using Super.DomainService;


public class LinkHelper
{

    static List<string> NewAddLink = new List<string>();
    public static string SetAction(string domain, string controller, string action)
    {
        var lkt = typeof(Link);

        string pName = domain == "WWW" ? "Main" : domain;

        bool isExitsDomain, isExitsController = false;

        var dt = lkt.GetProperty(pName);


#pragma warning disable 665
        //判断domain 
        if (isExitsDomain = dt != null || NewAddLink.Exists(j => j == domain))
        {
            var ct = dt == null ? null : dt.GetValue(null, null).GetType().GetProperty(controller);

            //判断Controller
            if (isExitsController = ct != null || NewAddLink.Exists(j => j == domain + "," + controller))
            {
                var at = ct == null ? null : ct.GetValue(dt.GetValue(null, null), null).GetType().GetProperty(action);

                //判断Action
                if (at != null || NewAddLink.Exists(j => j == domain + "," + controller + "," + action))
                    return "";
            }
        }
#pragma warning disable 665


        //if (NewAddLink.Exists(j => j == "{0},{1},{2}".FormatMe(domain, controller, action)))
        //{
        //    return "";
        //}

        NewAddLink.Add(domain);
        NewAddLink.Add(domain + "," + controller);
        NewAddLink.Add(domain + "," + controller + "," + action);
        //NewAddLink.Add("{0},{1},{2}".FormatMe(domain, controller, action));

        var s = @"public partial class Link
{
    public partial class _" + pName + @"LinkInfoDomain:BaseLinkInfo
    {
        public partial class " + controller + @"LinkInfoController:BaseLinkInfo
        {
            public partial class " + action + @"LinkInfoAction:BaseLinkInfo{}

            private " + action + "LinkInfoAction _" + action + @" = new " + action + @"LinkInfoAction{ Path=""http://" + domain + BangKeService.Domain.Root.ToString() + "/" + controller + "/" + action + @"""};

            public " + action + "LinkInfoAction " + action + @" { get { return _" + action + @"; } }

        }" + (isExitsController ? "" : @"

        private " + controller + "LinkInfoController _" + controller + " = new " + controller + @"LinkInfoController{ Path=""http://" + domain + BangKeService.Domain.Root.ToString() + "/" + controller + @""" };" + @"
        public " + controller + "LinkInfoController " + controller + " { get { return _" + controller + "; } }") + @"
    }" + (isExitsDomain ? "" : @"
    private static _" + pName + "LinkInfoDomain _" + pName + " = new _" + pName + @"LinkInfoDomain{Path = ""http://" + domain + BangKeService.Domain.Root.ToString() + @"""};" + @"
    public static _" + pName + "LinkInfoDomain " + pName + " { get { return _" + pName + "; } }") + @"
}

";
        return s;
    }

    public static void Init(Assembly assembly)
    {
        var assemblyName = assembly.ManifestModule.Name.Replace("Web", "").Replace(".DLL", "").Replace(".dll", "").Replace("BK.", "");
        if (assemblyName == "")
        {
            assemblyName = "WWW";
        }
        else if (assemblyName == "UserCenter")
        {
            assemblyName = "User";
        }
        else if ("Admin,Resources".SplitExits(assemblyName))
        {
            return;
        }
        else if (assemblyName.IndexOf(".") >= 0)
        {
            //这是个问题.
            throw new Exception("发现问题!\r\n 异常的程序集名称 " + assemblyName + "\r\n\r\n By:承志");
        }

        var sb = new StringBuilder();

        var cls = assembly.GetTypes().Where(j => typeof(Controller).IsAssignableFrom(j) && j.Name != "BaseController").ToList();

        foreach (var cl in cls)
        {
            var mds = cl.GetMethods().Where(j => j.ReturnType == typeof(ActionResult));

            var cname = cl.Name.Remove(cl.Name.Length - 10);

            foreach (var md in mds)
            {
                //过滤掉NoFind 和 Ajax 方法
                var fl = md.GetCustomAttributes(typeof(LinkFilter), false).FirstOrDefault() as LinkFilter;
                if (fl != null && !fl.IsIntellisense)
                    continue;

                if ("_Ajax_Json,NotFound".SplitExits(md.Name))
                    continue;

                sb.Append(SetAction(assemblyName, cname, md.Name));
            }
        }

        var s = sb.ToString();

        if (s.Length > 10)
        {
            try
            {
                System.IO.File.AppendAllText(_UrlLinkIndex.GetFile(), s);
            }
            catch (Exception ex)
            {
                throw new Exception("由于新增了页面! \r\n\r\nService/Link/LinkClass.cs 文件需要签出! By:承志");
            }
        }
    }

}